package com.rules.admin.publish;


import com.rules.admin.entity.RulePackage;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.imps.CuratorFrameworkState;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.CloseableUtils;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZookeeperNotifer implements Notifier {
   private String zookeeperConnectionStr;
   private String ruleBasePath;
   private CuratorFramework curatorFramework;
   private String nameSpace;
   private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperNotifer.class);

   public void notify(RulePackage rulePackage) {
      try {
         if (CuratorFrameworkState.STARTED != this.curatorFramework.getState()) {
            this.curatorFramework.start();
         }

         String groupId = rulePackage.getGroupId();
         String artifactId = rulePackage.getArtifactId();
         String version = rulePackage.getVersion();
         StringBuilder data = new StringBuilder();
         data.append(artifactId).append(":").append(version);
         String path = this.ruleBasePath + "/" + rulePackage.getArtifactId();
         Stat stat = (Stat)this.curatorFramework.checkExists().forPath(path);
         if (stat == null) {
            this.curatorFramework.create().creatingParentsIfNeeded().forPath(path, data.toString().getBytes());
            if (LOGGER.isDebugEnabled()) {
               LOGGER.debug("ADD NODE: PATH:" + path + ",DATA:" + data);
            }
         } else {
            this.curatorFramework.setData().forPath(path, data.toString().getBytes());
            if (LOGGER.isDebugEnabled()) {
               LOGGER.debug("UPDATE NODE: PATH:" + path + ",DATA:" + data);
            }
         }
      } catch (Exception var8) {
         LOGGER.error("notify error:" + var8.getMessage(), var8);
      }

   }

   public void deleteNotify(RulePackage rulePackage) {
      try {
         String groupId = rulePackage.getGroupId();
         String path = this.ruleBasePath + "/" + groupId + "/" + rulePackage.getArtifactId();
         this.curatorFramework.checkExists().forPath(path);
         this.curatorFramework.delete().guaranteed().forPath(path);
         if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("DELETE NODE: PATH:" + path);
         }
      } catch (Exception var7) {
         LOGGER.error("deleteNotify error:" + var7.getMessage(), var7);
      } finally {
         CloseableUtils.closeQuietly(this.curatorFramework);
      }

   }

   public void holidaysPublish() {
      String path = this.ruleBasePath + "/holidays";
      String data = "holidays:0";

      try {
         Stat stat = (Stat)this.curatorFramework.checkExists().forPath(path);
         if (stat == null) {
            this.curatorFramework.create().creatingParentsIfNeeded().forPath(path, data.toString().getBytes());
         } else {
            String[] str = (new String((byte[])this.curatorFramework.getData().forPath(path))).split(":");
            data = "holidays:" + String.valueOf(Integer.valueOf(str[1]) + 1);
            this.curatorFramework.setData().forPath(path, data.toString().getBytes());
         }
      } catch (Exception var5) {
         var5.printStackTrace();
      }

   }

   private void init() {
      if (this.zookeeperConnectionStr != null) {
         RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, Integer.MAX_VALUE);
         this.curatorFramework = CuratorFrameworkFactory.newClient(this.zookeeperConnectionStr, retryPolicy);
         if (this.nameSpace != null && "".equals(this.nameSpace)) {
            this.curatorFramework.usingNamespace(this.nameSpace);
         }

         this.curatorFramework.start();
         LOGGER.info("ZookeeperNotifer init");
      } else {
         LOGGER.info("zookeeperConnectionStr cannot be null");
      }

   }

   public void setZookeeperConnectionStr(String zookeeperConnectionStr) {
      this.zookeeperConnectionStr = zookeeperConnectionStr;
   }

   public void setRuleBasePath(String ruleBasePath) {
      this.ruleBasePath = ruleBasePath;
   }

   public void setNameSpace(String nameSpace) {
      this.nameSpace = nameSpace;
   }

   public static void main(String[] args) {
   }
}
